-- 扶植圣女
local fuzhishengnv = fk.CreateSkill {
    name = "mcdh_fuzhishengnv",
    frequency = Skill.Compulsory,
    on_acquire =  function(self,player, is_start)
        local room = player.room
        room:setPlayerMark(player,"@mcdh_fuzhishengnv",{"#mcdh_fuzhishengnv_1","#mcdh_fuzhishengnv_2","#mcdh_fuzhishengnv_3"})
    end,
    on_lose = function(self,player)
        local room = player.room
        room:setPlayerMark(player,0)
    end
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

fuzhishengnv:addEffect(fk.EventPhaseStart,{
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(self) and target == player and target.phase == Player.Finish and #player:getTableMark("@mcdh_fuzhishengnv") > 0 then
            local room = player.room
            local all_choices = player:getTableMark("@mcdh_fuzhishengnv")
            local choices = {}
            if table.contains(all_choices,"#mcdh_fuzhishengnv_1") then
                if not player:isNude() then
                    table.insert(choices,"#mcdh_fuzhishengnv_1")
                end
            end
            if table.contains(all_choices,"#mcdh_fuzhishengnv_2") then
                if player.maxHp > 0 then
                    table.insert(choices,"#mcdh_fuzhishengnv_2")
                end
            end
            if table.contains(all_choices,"#mcdh_fuzhishengnv_3") then
                if player:hasSkill("mcdh_youjiebizhu") and #table.filter(room:getOtherPlayers(player),function (p)
                    return not p:hasSkill("mcdh_youjiebizhu")
                end) > 0 then
                    table.insert(choices,"#mcdh_fuzhishengnv_3")
                end
            end
            return #choices > 0
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local all_choices = player:getTableMark("@mcdh_fuzhishengnv")
        local choices = {}
        if table.contains(all_choices,"#mcdh_fuzhishengnv_1") then
            if not player:isNude() then
                table.insert(choices,"#mcdh_fuzhishengnv_1")
            end
        end
        if table.contains(all_choices,"#mcdh_fuzhishengnv_2") then
            if player.maxHp > 0 then
                table.insert(choices,"#mcdh_fuzhishengnv_2")
            end
        end
        if table.contains(all_choices,"#mcdh_fuzhishengnv_3") then
            if player:hasSkill("mcdh_youjiebizhu") and #table.filter(room:getOtherPlayers(player),function (p)
                return not p:hasSkill("mcdh_youjiebizhu")
            end) > 0 then
                table.insert(choices,"#mcdh_fuzhishengnv_3")
            end
        end
        if #choices > 0 then
            local choose = room:askToChoice(player,{choices=choices,all_choices=all_choices,skill_name=self.name})
            if choose == "#mcdh_fuzhishengnv_1" then
                local tars,card = room:askToChooseCardsAndPlayers(player,{targets=room:getOtherPlayers(player),min_num=1,max_num=1,min_card_num=1,max_card_num=1,pattern= ".|.|.|hand,equip",skill_name=self.name,cancelable=false,prompt="#mcdh_fuzhishengnv_1_askfor"})
                room:moveCardTo(card, Card.PlayerHand, tars[1], fk.ReasonGive, self.name, nil, false, player.id)
            elseif choose == "#mcdh_fuzhishengnv_2" then
                local tar = room:askToChoosePlayers(player,{targets=room:getOtherPlayers(player),min_num=1,max_num=1,prompt="#mcdh_fuzhishengnv_3_askfor",skill_name=self.name,cancelable=false})
                room:changeMaxHp(player, -1)
                room:changeMaxHp(tar[1], 1)
            elseif choose == "#mcdh_fuzhishengnv_3" then
                local tar = room:askToChoosePlayers(player,{targets=table.filter(room:getOtherPlayers(player),function (p)
                    return not p:hasSkill("mcdh_youjiebizhu")
                end),min_num=1,max_num=1,prompt="#mcdh_fuzhishengnv_2_askfor",skill_name=self.name,cancelable=false})
            
                room:handleAddLoseSkills(player, "-mcdh_youjiebizhu", nil)
                room:handleAddLoseSkills(tar[1], "mcdh_youjiebizhu", nil)
            end
            table.removeOne(all_choices,choose)
            room:setPlayerMark(player,"@mcdh_fuzhishengnv",all_choices)
            if #all_choices == 2 then
                room:handleAddLoseSkills(player, "mcdh_ref_ganglie", nil)
            elseif #all_choices == 1 then
                room:handleAddLoseSkills(player, "mcdh_ref_wenji", nil)
            elseif #all_choices == 0 then
                room:handleAddLoseSkills(player, "mcdh_ref_mubing", nil)
            end
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_fuzhishengnv"] = "扶圣",
    [":mcdh_fuzhishengnv"] = "『扶植圣女』<br>锁定技，结束阶段，你执行以下一项并删除该项：1.交给其他角色一张牌；2.减少1点体力上限并令一名其他角色增加1点体力上限；"..
            "3.失去〖节诛〗并令一名其他角色获得〖节诛〗。每删除一项，依次获得〖刚烈〗〖问计〗〖募兵〗",
    ["#mcdh_fuzhishengnv_1"] = "1",
    ["#mcdh_fuzhishengnv_2"] = "2",
    ["#mcdh_fuzhishengnv_3"] = "3",
    [":#mcdh_fuzhishengnv_1"] = "交给其他角色一张牌",
    [":#mcdh_fuzhishengnv_2"] = "减少1点体力上限并令一名其他角色增加1点体力上限",
    [":#mcdh_fuzhishengnv_3"] = "失去〖节诛〗并令一名其他角色获得〖节诛〗",
    ["#mcdh_fuzhishengnv_1_askfor"] = "扶植圣女：选择一名其他角色，交给其一张牌",
    ["#mcdh_fuzhishengnv_2_askfor"] = "扶植圣女：选择一名其他角色，令其增加1点体力上限且你减少1点体力上限",
    ["#mcdh_fuzhishengnv_3_askfor"] = "扶植圣女：选择一名其他角色，令其获得〖节诛〗且你失去〖节诛〗",
    ["@mcdh_fuzhishengnv"] = "扶圣"
}

return fuzhishengnv